package cn.wolfcode.shop.controller;

import java.util.List;
import java.util.Map;

import org.springframework.core.OrderComparator.OrderSourceProvider;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;

import cn.wolfcode.shop.domain.OrderAction;
import cn.wolfcode.shop.domain.OrderInfo;
import cn.wolfcode.shop.domain.UserInfo;
import cn.wolfcode.shop.qo.OrderQueryObject;
import cn.wolfcode.shop.qo.PageResult;
import cn.wolfcode.shop.service.IOrderActionService;
import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.service.IUserService;
import cn.wolfcode.shop.vo.JSONResult;
import cn.wolfcode.shop.vo.OrderStatusChangeVo;

/**
 * Created by luohaipeng on 2018/2/23.
 */
@Controller
@RequestMapping(value = "/orders")
public class OrderInfoController {

	@Reference
	IOrderInfoService orderInfoService;
	@Reference
	IUserService userService;
	@Reference
	IOrderActionService orderActionService;

	/**
	 * /orders/view
	 * GET
	 * 进入订单管理界面
	 * @return
	 */
	@GetMapping(value = "/view")
	public String orderInfo() {

		return "order/order_info";
	}

	/**
	 * /orders
	 * GET
	 * @param map
	 * @param qo
	 * @return
	 */
	@GetMapping(value = "")
	public String page(Map map, OrderQueryObject qo) {
		PageResult page = orderInfoService.query(qo);
		map.put("page", page);
		return "order/order_list";
	}

	/**
	 * /orders/{id}
	 * GET
	 * @param id
	 * @param map
	 * @return
	 */
	@GetMapping(value = "/{id}")
	public String detail(@PathVariable("id") Long id, Map map) {
		OrderInfo orderInfo = orderInfoService.getById(id);
		UserInfo userInfo = userService.getById(orderInfo.getUserId());
		List<OrderAction> orderActionList = orderActionService.getOrderAction(id);
		map.put("userInfo", userInfo);
		map.put("orderInfo", orderInfo);
		map.put("orderActionList", orderActionList);
		return "order/order_detail";
	}

	/**
	 * 修改订单状态
	 * @return
	 */
	@PostMapping("/{orderId}")
	@ResponseBody
	public JSONResult changeStatus(@PathVariable Long orderId, OrderStatusChangeVo vo) {
		vo.setOrderId(orderId);
		vo.setPlace("后台");
		vo.setUserName("admin");
		orderInfoService.changeOrderStatus(vo);
		return new JSONResult();
	}

}
